package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.pojo.EduChapter;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.pojo.EduVideo;
import com.atguigu.eduservice.pojo.chapter.ChapterVo;
import com.atguigu.eduservice.pojo.chapter.VideoVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-20
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService videoService;//注入小节



    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //进行实现，根据课程id查询出所有的课程章节
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapper);
        //查询小节
        QueryWrapper<EduVideo> WrapperVideo = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        List<EduVideo> eduVideoList = videoService.list(WrapperVideo);
        //创建List集合用于封装数据
        List<ChapterVo> finalList = new ArrayList<>();
        //遍历查询出来的集合
        //遍历查询出来的章节
        for (EduChapter eduChapter : eduChapterList) {
            //每个章节
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //把chapter放到最终list集合

            finalList.add(chapterVo);
            List<VideoVo>videoVos = new ArrayList<>();
            for (EduVideo eduVideo : eduVideoList) {
                //得到每个小节
                //判断,创建集合封装章节中的小节
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVos.add(videoVo);
                }
            }
            //将封装好的小节list集合，放在章节对象李
            chapterVo.setChildren(videoVos);
        }

        return finalList;

    }
//删除章节的方法
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据章节id查询小节表，如果能查询出数据就不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = videoService.count(wrapper);
        //判断，如果值有数据
        if (count > 0){//查询出小节
            throw new GuliException(20001,"不能删除，他还有小节呢!");
        }else{//不能查询出数据，才进行删除
            //删除章节
            int result = baseMapper.deleteById(chapterId);
            //成功就是true，失败就是0
            return result>0;
        }

    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        //删除章节
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }
}

